from typing import Any, Text, Dict, List
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
from .neo4j_connection import graph
from .api_instance import api_instance

class ActionAskAboutEvent(Action):
    def name(self) -> Text:
        return "action_ask_about_event"

    async def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
        event_name = next(tracker.get_latest_entity_values("event"), None)
        event_list = graph.query_event(event_name)

        if not event_name:
            api_instance.ask(tracker.events, dispatcher)
            return []

        if not event_list:
            dispatcher.utter_message("不好意思，我不清楚你问的" + (event_name if event_name else "") + "这件事。", attachment="finish")
            return []

        event_node = event_list[0]["e"]
        message = (event_name
                   + ("发生于" + event_node["time"] + "。那时" if "time" in event_node else "当时")
                   + event_node["des"])
        api_instance.optimize_response(tracker.events, message, dispatcher)
        return []
